﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;

	public class PollTrackingService : IPollTrackingService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="PollTrackingService"/> class.
		/// </summary>
		/// <param name="identityService">The identity service.</param>
		/// <param name="pollTrackingRepository">The poll tracking repository.</param>
		public PollTrackingService(
			IIdentityService identityService, 
			IPollTrackingRepository pollTrackingRepository)
		{
			this.IdentityService = identityService;
			this.PollTrackingRepository = pollTrackingRepository;
		}

		/// <summary>
		/// Gets or sets the identity service.
		/// </summary>
		/// <value>The identity service.</value>
		public IIdentityService IdentityService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the poll tracking repository.
		/// </summary>
		/// <value>The poll tracking repository.</value>
		public IPollTrackingRepository PollTrackingRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the poll tracking by the specified specification.
		/// </summary>
		/// <param name="specification">The specification.</param>
		/// <returns></returns>
		public PollTracking GetBy(PollTrackingSpecification specification)
		{
			return this.PollTrackingRepository.GetBy(specification);
		}

		/// <summary>
		/// Creates the poll tracking.
		/// </summary>
		/// <returns></returns>
		public PollTracking Create()
		{
			return new PollTracking
			{
				Created = DateTime.Now
			};
		}

		/// <summary>
		/// Inserts the specified poll tracking.
		/// </summary>
		/// <param name="tracking">The tracking.</param>
		public void Insert(PollTracking tracking)
		{
			this.PollTrackingRepository.Insert(tracking);
		}

		/// <summary>
		/// Inserts the specified poll tracking.
		/// </summary>
		/// <param name="choice">The choice.</param>
		public void Insert(PollChoice choice)
		{
			var tracking = this.Create();

			tracking.PollId = choice.PollId;
			tracking.ChoiceId = choice.Id;
			tracking.UserId = IdentityService.Current.Id;

			this.Insert(tracking);
		}

		/// <summary>
		/// Deletes the specified poll tracking.
		/// </summary>
		/// <param name="tracking">The tracking.</param>
		public void Delete(PollTracking tracking)
		{
			this.PollTrackingRepository.Delete(tracking);
		}
	}
}